package cn.biplam.back.services.dataoperator;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import cn.biplam.common.interfaces.DataOperatorServer;
import cn.biplam.common.tools.Call;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.sqlresult.SqlResult;

/**
 * 数据操作层编码交换接口实现类
 * @author WangSong 2009-9-24下午12:13:39
 * @version 1.0
 */
public class DataOperatorServerImpl extends UnicastRemoteObject implements DataOperatorServer{
	
	private static final long serialVersionUID = 2238245953653790858L;
	private Lock mLock=new ReentrantLock();
	private int iCount=0;
	
	/**
	 * 构造方法
	 * @throws RemoteException
	 */
	public DataOperatorServerImpl() throws RemoteException {
		iCount++;
	}
	
	/**
	 * 返回实现类的接口对象
	 * @param className 接口的实现类名称
	 * @return 实现类的接口对象
	 * @throws RemoteException
	 * @throws ErrorMessage
	 */
	public Object get(String className) 
	                       throws RemoteException,ErrorMessage{
		Object obj = null;
		Call call = null;
		addCount();
		try{
			String[] classPath = getClassByOptId(className);
			if(classPath==null || classPath[0]==null){
				throw new ErrorMessage("DataOperatorServerImpl error,className is not found:"+className);
			}else if(!"1".equals(classPath[1])){
				/**判断模型是否发布*/
				throw new ErrorMessage("DataOperatorServerImpl error,class is not authorized:"+className);
			}
			
			try{
				call = new Call(classPath[0]);
				if(call!=null){
					obj = call.getInstance();
				}
			}catch(ErrorMessage e){
				if(e.getMessage()==null){
					e.setMessage("\n\tat DataOperatorServerImpl,class is:"+classPath[0]);
				}else{
					e.setMessage(e.getMessage()+"\n\tat DataOperatorServerImpl,class is:"+classPath[0]);
				}
				throw e;
			}	
		}catch(ErrorMessage e){
			throw e;
		}finally{
			reduceCount();
		}
		return obj;
	}
	
	/**
	 * 返回实现类的接口对象
	 * @param className 接口的实现类名称
	 * @param classParams  构造方法参数
	 * @return 实现类的接口对象
	 * @throws RemoteException
	 * @throws ErrorMessage
	 */
	public Object get(String className,Object... classParams) 
	                       throws RemoteException,ErrorMessage{
		Object obj = null;
		Call call = null;
		String classPath[] = null;
		addCount();
		try{
			classPath = getClassByOptId(className);
			if(classPath==null || classPath[0]==null){
				throw new ErrorMessage("DataOperatorServerImpl error,className is not found:"+className);
			}else if(!"1".equals(classPath[1])){
				/**判断模型是否发布*/
				throw new ErrorMessage("DataOperatorServerImpl error,class is not authorized:"+className);
			}
			checkParams(classPath[0], classParams);
			try{
				call = new Call(classPath[0],classParams);
				if(call!=null){
					obj = call.getInstance();
				}
			}catch(ErrorMessage e){
				if(e.getMessage()==null){
					e.setMessage("\n\tat DataOperatorServerImpl,class is:"+classPath[0]+"。params is:"+getParams(classParams));
				}else{
					e.setMessage(e.getMessage()+"\n\tat DataOperatorServerImpl,class is:"+classPath[0]);
				}
				throw e;
			}	
		}catch(ErrorMessage e){
			throw e;
		}finally{
			reduceCount();
		}
		return obj;
	}
	
	/**
	 * 参数检查
	 * @param classParams
	 * @throws ErrorMessage 
	 */
	private void checkParams(String classPath,Object[] classParams) throws ErrorMessage{
		if(classParams==null){
			throw new ErrorMessage("DataOperatorServerImpl instance class failed，params is null:"+classPath);
		}
		for(int i=0;i<classParams.length;i++){
			if(classParams[i]==null){
				throw new ErrorMessage("DataOperatorServerImpl instance class failed，parmars num:"+(i+1)+" is null:"+classPath);
			}
		}
	}
	
	private String getParams(Object[] classParams){
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<classParams.length;i++){
			if(i!=0){
				sb.append(",");
			}
			sb.append("参数");
			sb.append(i+1);
			sb.append(":");
			sb.append(classParams[i]);
		}
		return sb.toString();
	}
	
	/**
	 * 根据行为编码来查询实现类
	 * @param actionId 行为编码id
	 * @return 实现类
	 * @throws ErrorMessage 
	 */
	private String[] getClassByOptId(String optId) throws ErrorMessage{
		
		SqlResult sr = new DataOperatorServiceOpt().getById(optId);
		if(TOOLS.exist(sr)){
			return new String[]{sr.get(0,"s_class_path"),sr.get(0,"publish")};
		}else{
			throw new ErrorMessage("DataOperatorServerImpl instance class failed,className is not found:"+optId);
		}
	}

	/**服务监控用*/
	public int getCount() throws RemoteException {
		return this.iCount;
	}

	private synchronized void addCount() {
		this.iCount++;
	}

	private synchronized void reduceCount() {
		this.iCount--;
	}

	public void lock() throws RemoteException {
		mLock.lock();
	}

	public boolean trylock() throws RemoteException {
		return mLock.tryLock();
	}

	public void unlock() {
		mLock.unlock();
	}
}
